package cn.wps.moffice.demo.menu;

import java.util.List;

import android.app.DialogFragment;
import android.database.DataSetObserver;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.TextView;
import cn.wps.moffice.demo.R;
import cn.wps.moffice.demo.util.Define;
import cn.wps.moffice.demo.util.SettingPreference;

/**
 * @author ：chenyunbiao
 */
public class ExListAdapter extends BaseExpandableListAdapter {
	
	SettingPreference preference;
	private boolean IsSelected[][] = new boolean[3][3];
	private ParamActivity mContext;
	private List<Groups> groups;
	private SparseArray<List<Child>> childs;
	
	private final String[][] SETTING = {
			{
				Define.OPEN_MODE,
				//Define.IS_CUSTOM_MENU,
				Define.IS_SHOW_VIEW
			},
			{
				Define.SEND_SAVE_BROAD,
				Define.SEND_CLOSE_BROAD,
				Define.ENTER_REVISE_MODE
			},
			{
				Define.AT_SAVE,
				Define.AT_COPY,
				Define.AT_SHARE
			}
			
	};
	
	public ExListAdapter(ParamActivity context, List<Groups> groups, SparseArray<List<Child>> childsData) {
		this.mContext = context;
		this.groups = groups;
		this.childs = childsData;
		preference = new SettingPreference(context);
		setIsSelected();
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		//每组里面子项的视图
		final ChildHolder childHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.child, parent, false);
			childHolder = new ChildHolder();
			childHolder.title = (TextView) convertView.findViewById(R.id.itemTitle);
			childHolder.content = (TextView) convertView.findViewById(R.id.itemText);
			childHolder.check = (CheckBox) convertView.findViewById(R.id.itemButton);
			convertView.setTag(childHolder);
			childHolder.check.setChecked(IsSelected[groupPosition][childPosition]);
		} else {
			childHolder = (ChildHolder) convertView.getTag();
			childHolder.check.setVisibility(View.INVISIBLE);  //复用后要重新将checkbox设为默认不可见，否则会复用之前checkbox的状态
			childHolder.check.setChecked(IsSelected[groupPosition][childPosition]);
		}
		childHolder.title.setText(childs.get(groupPosition).get(childPosition).getTitle());
		childHolder.content.setText(childs.get(groupPosition).get(childPosition).getContent());
		if (childs.get(groupPosition).get(childPosition).isCheckBox()) {
			childHolder.check.setVisibility(View.VISIBLE);
		}
		convertView.setOnClickListener(new ClickItem(childHolder, groupPosition, childPosition));
		return convertView;
	}
	
	private void setIsSelected() 
	{
		IsSelected[0][0] = preference.getSettingParam("", false);
		//IsSelected[0][1] = preference.getSettingParam(Define.IS_CUSTOM_MENU, true);
		IsSelected[0][2] = preference.getSettingParam(Define.IS_SHOW_VIEW, true);
		
		IsSelected[1][0] = preference.getSettingParam(Define.SEND_SAVE_BROAD, false);
		IsSelected[1][1] = preference.getSettingParam(Define.SEND_CLOSE_BROAD, false);
		IsSelected[1][2] = preference.getSettingParam(Define.ENTER_REVISE_MODE, false);
		
		IsSelected[2][0] = preference.getSettingParam(Define.AT_SAVE, true);
		IsSelected[2][1] = preference.getSettingParam(Define.AT_COPY, true);
		IsSelected[2][2] = preference.getSettingParam(Define.AT_SHARE, true);
	}
	
	class ClickItem implements OnClickListener {
		
		ChildHolder childHolder;
		int groupPos;
		int childPos;
		
		public ClickItem(ChildHolder childHolder, int groupPos, int childPos) {
			this.childHolder = childHolder;
			this.groupPos = groupPos;
			this.childPos = childPos;
		}
		@Override
		public void onClick(View v) {
			if (groupPos == 0 && childPos == 0) {
				showCustomDialog(2);
			} else {
				childHolder.check.performClick();
				preference.setSettingParam(SETTING[groupPos][childPos], childHolder.check.isChecked());
			}
			
		}
	}
	
	private void showCustomDialog(int type) {
		DialogFragment newFragment = AlertDialogFragment.newInstance(type);
		newFragment.show(mContext.getFragmentManager(), "dialog");
	}
	
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		//分组的视图
		GroupHolder groupHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(R.layout.groups, parent, false);
			groupHolder = new GroupHolder();
			groupHolder.title = (TextView) convertView.findViewById(R.id.textGroup);
			convertView.setTag(groupHolder);
			
		} else {
			groupHolder = (GroupHolder) convertView.getTag();
		}
		groupHolder.title.setText(groups.get(groupPosition).getTitle());
		return convertView;
	}
	
	@Override
	public boolean areAllItemsEnabled() {
		return true;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childs.get(groupPosition).get(childPosition).getTitle();
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}
	
	@Override
	public int getChildrenCount(int groupPosition) {
		return childs.get(groupPosition).size();
	}

	@Override
	public long getCombinedChildId(long groupId, long childId) {
		return 0;
	}

	@Override
	public long getCombinedGroupId(long groupId) {
		return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groups.get(groupPosition).getTitle();
	}

	@Override
	public int getGroupCount() {
		return groups.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		
	}

	@Override
	public void registerDataSetObserver(DataSetObserver observer) {
		
	}

	@Override
	public void unregisterDataSetObserver(DataSetObserver observer) {
		
	}
	
	class GroupHolder {
		TextView title;
	}
	
	class ChildHolder {
		TextView title;
		TextView content;
		CheckBox check;
	}
	
}
